#!/bin/bash
# Create a new tmux session
session_name="process_$(date +%s)"
tmux new-session -d -s $session_name

# Split the window into four panes
tmux selectp -t 0    # select the first (0) pane
tmux splitw -h -p 50 # split it into two halves
tmux selectp -t 0    # select the first (0) pane
tmux splitw -v -p 50 # split it into two halves

tmux selectp -t 2    # select the new, second (2) pane
tmux splitw -v -p 50 # split it into two halves
tmux selectp -t 0    # go back to the first pane

# Run roscore in the first pane
tmux select-pane -t 0
tmux send-keys "roscore" Enter

# Run the data_stream_get.py script in the second pane
tmux select-pane -t 1
tmux send-keys "python3 ./scripts/data_stream_get.py" Enter

# Run the map_saver command in the third pane
tmux select-pane -t 2
tmux send-keys "sleep $(rosbag info $1 | awk -F '[()]' '/duration.*?s\)/ {print ($2 - 3)}')" Enter
tmux send-keys "rosrun map_server map_saver -f ./map/test" Enter

# Play the ros bag in the fourth pane
tmux select-pane -t 3
tmux send-keys "rosbag play $1" Enter

# Attach to the tmux session
tmux -2 attach-session -t $session_name